package com.github.refresh;

import android.content.res.TypedArray;
import android.graphics.PointF;

public class RefreshLayoutHelper {
    private int headerViewHeight;
    private PointF preEventPoint = new PointF();
    /*偏移量*/
    private float offsetX;
    private float offsetY;
    /*当前总的偏移量，如果大于0，说明可以向上滑动子view*/
    private volatile int currentTotalOffsetY;
    /*改变偏移量之前的偏移量*/
    private volatile int preTotalOffsetY;


    //true:释放刷新，false下拉到一定距离刷新
    private boolean releaseRefresh;
    /*下拉阻力，1：无阻力*/
    private float pullDownResistance = 1.5f;
    //下拉头view高度刷新的距离和头view高度的比例
    private float pullDownHeaderHeightRefreshRatio = 1.5f;
    //下拉头view距离是多少时刷新
    private int pullDownHeaderHeightRefresh;
    //下拉头view距离超过多少比例时不刷新
    private float pullDownHeaderHeightOpenPageRatio = 0;
    //下拉头view距离超过多少时不刷新
    private int pullDownHeaderHeightOpenPage;
    /*打开其他页面头viw回弹时间*/
    private long durationCloseForOpenPage = 300;
    //释放刷新时，回弹至头view高度的时间
    private long durationClose = 390;
    //头布局回弹的时间
    private long durationCloseHeader = 500;
    //刷新时是否显示头view
    private boolean refreshKeepHeader = true;
    //是否显示刷新成功状态的view
    private boolean refreshShowSuccess = true;
    //是否显示刷新失败状态的view
    private boolean refreshShowError = true;
    private long refreshResultDurationTime = 500;
    //是否处理子view横向滑动的情况
    private boolean needHorizontalScroll = true;
    //x和y的比例是多少时横向滑动
    private float horizontalScrollXYRatio = 1.02f;


    public RefreshLayoutHelper() {

    }

    protected void setAttr(TypedArray typedArray) {
        if (typedArray == null) {
            return;
        }
        releaseRefresh = typedArray.getBoolean(R.styleable.RefreshLayout_releaseRefresh, true);
        pullDownResistance = typedArray.getFloat(R.styleable.RefreshLayout_pullDownResistance, pullDownResistance);
        pullDownHeaderHeightRefreshRatio = typedArray.getFloat(R.styleable.RefreshLayout_pullDownHeaderHeightRefreshRatio, pullDownHeaderHeightRefreshRatio);
        pullDownHeaderHeightRefresh = typedArray.getDimensionPixelSize(R.styleable.RefreshLayout_pullDownHeaderHeightRefresh, 0);
        pullDownHeaderHeightOpenPageRatio = typedArray.getFloat(R.styleable.RefreshLayout_pullDownHeaderHeightOpenPageRatio, 0);
        pullDownHeaderHeightOpenPage = typedArray.getDimensionPixelSize(R.styleable.RefreshLayout_pullDownHeaderHeightOpenPage, 0);
        durationCloseForOpenPage = typedArray.getInteger(R.styleable.RefreshLayout_durationCloseForOpenPage, (int) durationCloseForOpenPage);
        durationClose = typedArray.getInteger(R.styleable.RefreshLayout_durationClose, (int) durationClose);
        durationCloseHeader = typedArray.getInteger(R.styleable.RefreshLayout_durationCloseHeader, (int) getDurationCloseHeader());
        refreshKeepHeader = typedArray.getBoolean(R.styleable.RefreshLayout_refreshKeepHeader, true);
        refreshShowSuccess = typedArray.getBoolean(R.styleable.RefreshLayout_refreshShowSuccess, true);
        refreshShowError = typedArray.getBoolean(R.styleable.RefreshLayout_refreshShowError, true);
        refreshResultDurationTime = typedArray.getInteger(R.styleable.RefreshLayout_refreshResultDurationTime, (int) getRefreshResultDurationTime());
        needHorizontalScroll = typedArray.getBoolean(R.styleable.RefreshLayout_needHorizontalScroll, true);
        horizontalScrollXYRatio = typedArray.getFloat(R.styleable.RefreshLayout_horizontalScrollXYRatio, horizontalScrollXYRatio);

    }

    public void move(float x, float y) {
        offsetX = x - preEventPoint.x;
        offsetY = y - preEventPoint.y;

        offsetY = offsetY / getPullDownResistance();

    }

    protected void setPreEventPoint(float x, float y) {
        this.preEventPoint.set(x, y);
    }

    protected boolean canMoveUp() {
        return currentTotalOffsetY > 0;
    }

    protected int getCurrentTotalOffsetY() {
        return currentTotalOffsetY;
    }

    protected void setCurrentTotalOffsetY(int currentTotalOffsetY) {
        if (currentTotalOffsetY <= 0) {
            /*这里赋值为0，否则开始第二次下拉的时候，preTotalOffsetY为上次下拉的值*/
            setPreTotalOffsetY(0);
        } else {
            setPreTotalOffsetY(this.currentTotalOffsetY);
        }
        this.currentTotalOffsetY = currentTotalOffsetY;
    }

    public int getPreTotalOffsetY() {
        return preTotalOffsetY;
    }

    public void setPreTotalOffsetY(int preTotalOffsetY) {
        this.preTotalOffsetY = preTotalOffsetY;
    }

    protected float getOffsetX() {
        return offsetX;
    }

    protected float getOffsetY() {
        return offsetY;
    }

    public int getHeaderViewHeight() {
        return headerViewHeight;
    }

    protected void setHeaderViewHeight(int headerViewHeight) {
        this.headerViewHeight = headerViewHeight;
        /*计算需要下拉多少高度刷新*/
        setPullDownHeaderHeightRefreshRatio(pullDownHeaderHeightRefreshRatio);
        /*计算需要多少高度打开其他页面*/
        /*如果pullDownHeaderHeightOpenPageRatio<=0且pullDownHeaderHeightOpenPage<=0则不需要下拉打开其他页面*/
        if (pullDownHeaderHeightOpenPageRatio > pullDownHeaderHeightRefreshRatio) {
            pullDownHeaderHeightOpenPage = (int) (headerViewHeight * pullDownHeaderHeightOpenPageRatio);
        }
        if (pullDownHeaderHeightOpenPage > 0) {
            if (pullDownHeaderHeightOpenPage <= pullDownHeaderHeightRefresh) {
                pullDownHeaderHeightOpenPage = (int) (pullDownHeaderHeightRefresh * 1.33f);
            }
        }
        setPullDownHeaderHeightOpenPage(pullDownHeaderHeightOpenPage);
    }

    public float getPullDownResistance() {
        if (pullDownResistance <= 0) {
            pullDownResistance = 1;
        }
        return pullDownResistance;
    }

    public void setPullDownResistance(float pullDownResistance) {
        this.pullDownResistance = pullDownResistance;
    }

    public boolean isReleaseRefresh() {
        return releaseRefresh;
    }

    public void setReleaseRefresh(boolean releaseRefresh) {
        this.releaseRefresh = releaseRefresh;
    }


    public void setPullDownHeaderHeightRefreshRatio(float pullDownHeaderHeightRefreshRatio) {
        this.pullDownHeaderHeightRefreshRatio = pullDownHeaderHeightRefreshRatio;

        if (pullDownHeaderHeightRefreshRatio <= 1) {
            pullDownHeaderHeightRefreshRatio = 1.5f;
        }
        if (headerViewHeight > 0 && pullDownHeaderHeightRefresh <= headerViewHeight) {
            pullDownHeaderHeightRefresh = (int) (headerViewHeight * pullDownHeaderHeightRefreshRatio);
        }
    }

    public float getPullDownHeaderHeightRefresh() {
        if (pullDownHeaderHeightRefresh <= headerViewHeight) {
            pullDownHeaderHeightRefresh = (int) (headerViewHeight * pullDownHeaderHeightRefreshRatio);
            if (pullDownHeaderHeightRefresh <= headerViewHeight) {
                pullDownHeaderHeightRefresh = (int) (headerViewHeight * 1.5f);
            }
        }
        return pullDownHeaderHeightRefresh;
    }

    public void setPullDownHeaderHeightRefresh(float pullDownHeaderHeightRefresh) {

        this.pullDownHeaderHeightRefresh = (int) pullDownHeaderHeightRefresh;
    }


    public void setPullDownHeaderHeightOpenPageRatio(float pullDownHeaderHeightOpenPageRatio) {
        this.pullDownHeaderHeightOpenPageRatio = pullDownHeaderHeightOpenPageRatio;
        /*如果pullDownHeaderHeightOpenPageRatio<=0且pullDownHeaderHeightOpenPage<=0则不需要下拉打开其他页面*/
        if (this.pullDownHeaderHeightOpenPageRatio > pullDownHeaderHeightRefreshRatio) {
            setPullDownHeaderHeightOpenPage((int) (headerViewHeight * pullDownHeaderHeightOpenPageRatio));
        }

    }

    public float getPullDownHeaderHeightOpenPage() {
        if(pullDownHeaderHeightOpenPage<=0&&pullDownHeaderHeightOpenPageRatio<=0){
            return 0;
        }
        if (this.pullDownHeaderHeightOpenPage <= pullDownHeaderHeightRefresh) {
            this.pullDownHeaderHeightOpenPage = (int) (pullDownHeaderHeightRefresh * pullDownHeaderHeightOpenPageRatio);
        }
        if (this.pullDownHeaderHeightOpenPage <= pullDownHeaderHeightRefresh) {
            this.pullDownHeaderHeightOpenPage = (int) (pullDownHeaderHeightRefresh * 1.33f);
        }
        return pullDownHeaderHeightOpenPage;
    }

    public void setPullDownHeaderHeightOpenPage(int pullDownHeaderHeightOpenPage) {
        this.pullDownHeaderHeightOpenPage = pullDownHeaderHeightOpenPage;
    }

    public long getDurationCloseForOpenPage() {
        if (durationCloseForOpenPage <= 0) {
            durationCloseForOpenPage = 100;
        }
        return durationCloseForOpenPage;
    }

    public void setDurationCloseForOpenPage(long durationCloseForOpenPage) {
        this.durationCloseForOpenPage = durationCloseForOpenPage;
    }

    public long getDurationClose() {
        if (durationClose <= 0) {
            durationClose = 100;
        }
        return durationClose;
    }

    public void setDurationClose(long durationClose) {
        this.durationClose = durationClose;
    }

    public long getDurationCloseHeader() {
        if (durationCloseHeader <= 0) {
            durationCloseHeader = 100;
        }
        return durationCloseHeader;
    }

    public void setDurationCloseHeader(long durationCloseHeader) {
        this.durationCloseHeader = durationCloseHeader;
    }

    public boolean isRefreshKeepHeader() {
        return refreshKeepHeader;
    }

    public void setRefreshKeepHeader(boolean refreshKeepHeader) {
        this.refreshKeepHeader = refreshKeepHeader;
    }

    public boolean isRefreshShowSuccess() {
        return refreshShowSuccess;
    }

    public void setRefreshShowSuccess(boolean refreshShowSuccess) {
        this.refreshShowSuccess = refreshShowSuccess;
    }

    public boolean isRefreshShowError() {
        return refreshShowError;
    }

    public void setRefreshShowError(boolean refreshShowError) {
        this.refreshShowError = refreshShowError;
    }

    public long getRefreshResultDurationTime() {
        if (refreshResultDurationTime <= 0) {
            refreshResultDurationTime = 200;
        }
        return refreshResultDurationTime;
    }

    public void setRefreshResultDurationTime(long refreshResultDurationTime) {
        this.refreshResultDurationTime = refreshResultDurationTime;
    }

    public boolean isNeedHorizontalScroll() {
        return needHorizontalScroll;
    }

    public void setNeedHorizontalScroll(boolean needHorizontalScroll) {
        this.needHorizontalScroll = needHorizontalScroll;
    }

    public float getHorizontalScrollXYRatio() {
        if (horizontalScrollXYRatio < 1) {
            horizontalScrollXYRatio = 1;
            return horizontalScrollXYRatio;
        }
        return horizontalScrollXYRatio;
    }

    public void setHorizontalScrollXYRatio(float horizontalScrollXYRatio) {
        this.horizontalScrollXYRatio = horizontalScrollXYRatio;
    }
}